<!-- 授权 -->
<template>
  <div>
    <a-modal
      :title="title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      width="40vw"
      @ok="handleOk"
      @cancel="handleCancel"
      :zIndex="100"
      :maskClosable="false"
    >
      <a-spin :spinning="confirmLoading">
        
        <j-form-container :disabled="false">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="用户授权" prop="userIds" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <select-user-by-dep :multi="true" placeholder="请选择用户授权" v-model="model.userIds"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="角色授权" prop="roleIds" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-select-role :multi="true" returnId placeholder="请选择角色授权" v-model="model.roleIds" :buttons="false"></j-select-role>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="组织机构授权" prop="orgIds" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-select-depart :multi="true" v-model="model.orgIds"/>
                </a-form-model-item>
              </a-col>
              
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import { postAction } from '@/api/manage'
import SelectUserByDep from './SelectUserByDep'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
export default {
  name: 'Authorize',
  data() {
    return {
      title: '授权',
      action: '', // 类型
      target: '', // 上一级目录的id值
      visible: false,
      confirmLoading: false,
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      model: {
        id: '',
        userIds: '', // 用户id
        roleIds: '', // 角色授权id
        orgIds: '' // 组织机构授权id
      },
      validatorRules: {
        userIds: [
          { required: false, message: '请选择用户!' },
        ],
        roleIds: [
          { required: false, message: '请选择角色!' }
        ],
        orgIds: [
          { required: false, message: '请选择组织机构!' }
        ],
      }
    };
  },
  components: {
    SelectUserByDep,
    JSelectDepart
  },
  methods: {
    edit(record) {
      this.model = Object.assign({}, record);
      this.$set(this.model, 'orgIds', record&&record.departIds?record.departIds : '')
      this.visible = true;
    },
    handleOk(e) {
      var that = this
      this.$refs.form.validate(valid => {
        if (valid) {
          // 判断三个里面要有一条数据才能通过
          if (this.model.userIds || this.model.orgIds || this.model.roleIds) {
            that.confirmLoading = true;
            let params = `id=${this.model.id}&userIds=${this.model.userIds||''}&roleIds=${this.model.roleIds || ''}&orgIds=${this.model.orgIds || ''}`
            let httpUrl = '/api/erp/oa/fileManager/auth?' + params
            postAction(httpUrl).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
                that.handleCancel()
              } else {
                that.$message.warning(res.message);
                that.handleCancel()
              }
            }).catch((error) => {
              // that.$emit('unButtonDisable');
              that.$message.warning(res.message);
              that.handleCancel()
              that.handleCancel()
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            that.$message.warning('请选择授权内容！')
          }
          
        }
      })
    },
    handleCancel(e) {
      this.model = {
        id: '',
        userIds: '', // 用户id
        roleIds: '', // 角色授权id
        orgIds: '' // 组织机构授权id
      },
      this.$refs.form.clearValidate();
      this.confirmLoading = false
      this.visible = false;
    },
  },
};
</script>